<template>
  <UploadExcel :on-success="handleSuccess" :before-upload="beforeUpload" />
</template>

<script>
// import { reqImportEmployee } from '@/api/employees'
import UploadExcel from '@/components/UploadExcel/index.vue'
export default {
  name: 'importexcel',
  computed: {},
  components:{
    UploadExcel
  },
  methods: {
    async handleSuccess({ header, results }) {
      // console.log(results)
      const userRelations = { // 传入对比值
        日期: 'timeOfEntry',
        手机号: 'mobile',
        姓名: 'username',
        转正: 'correctionTime',
        工号: 'workNumber'
      }
      const arr = []
      results.forEach((item) => {
        const userInfo = {}
        for (const key in item) {
          if (['timeOfEntry', 'correctionTime'].includes(userRelations[key])) {
            userInfo[userRelations[key]] = this.formatExcelDate(item[key], '-')
          } else {
            userInfo[userRelations[key]] = item[key]
          }
        }
        arr.push(userInfo)
      })
      console.log(arr) // 转换完的数据
      // 调用导入接口
      // await reqImportEmployee(arr) // 调用导入接口
      // this.$router.push('/employees')
    },
    formatExcelDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() - 1 + ''
      if (format && format.length === 1) {
        return (
          year +
          format +
          (month < 10 ? '0' + month : month) +
          format +
          (date < 10 ? '0' + date : date)
        )
      }
      return (
        year +
        (month < 10 ? '0' + month : month) +
        (date < 10 ? '0' + date : date)
      )
    },
    beforeUpload(file) {  // 大小限制
      // console.log('数据检测', file)
      const isLit1m = file.size / 1024 / 1024 < 1
      if (isLit1m) return true

      this.$message.success('成功')
      return false
    }
  }
}
</script>

<style lang="scss" scoped></style>
